﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;


public partial class Theme_DL_UserLogin : System.Web.UI.Page
{
    
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();

    PowerCreator.LMS.Web.DL.User.WebLearner opLearner = new PowerCreator.LMS.Web.DL.User.WebLearner();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ImageButton1.Attributes.Add("OnClick", "return chkForm()");

        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //登录信息
        PowerCreator.LMS.Web.DL.User.LoginInfo info = new PowerCreator.LMS.Web.DL.User.LoginInfo();

        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();

        if (!CheckPage()) return;

        info.LoginIp = PowerCreator.LMS.Common.Utils.IPUtils.GetIP;
        info.LoginTime = DateTime.Now;
        info.UserName = txt_UserName.Text;
        info.UserPwd = txt_UserPwd.Text;
        string UserLocation=GetUserLocation();


        if (opLearner.Login(info, ref login, ConfigurationManager.AppSettings["CheckLoginIP"].ToString(), UserLocation))
        {
            Session.Add(PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO, login);
            //Session["UserID"] = login.UserID;
            //Session["RealName"] = login.RealName;
            //Session["UserName"] = login.UserName;
            //opLearner.UpdateOnlineUserList();
            //string url = "Default.aspx";
            string url = "StudyCenter.aspx";
            PowerCreator.LMS.Common.Script.Script.RedirectURL(url);
        }
        else
        {
            //opLearner.AddOnlineUser(info, PowerCreator.LMS.Web.User.User.LoginNoUserID);
            txt_code.Text = "";
            PowerCreator.LMS.Common.Script.Script.Alert(Page, "errormsg", config.GetLanagerValueByKey(login.LoginMessage, PowerCreator.LMS.Language.LanagetItemType.MSG));
        }
    }
    private bool CheckPage()
    {
        if (string.IsNullOrEmpty(txt_code.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code1", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (Session["CheckCode"] != null)
        {
            string s = Session["CheckCode"].ToString();
            if (s.ToLower() != txt_code.Text.ToLower())
            {
                //lblLoginMsg.Text = config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page);
                PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code2", PowerCreator.LMS.Language.LanagetItemType.Page));
                txt_code.Text = "";
                //Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>AutoColseAler()</script>");
                return false;
            }
        }
        if (string.IsNullOrEmpty(txt_UserName.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code3", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        if (string.IsNullOrEmpty(txt_UserPwd.Text))
        {

            PowerCreator.LMS.Common.Script.Script.Alert(this, "msg", config.GetLanagerValueByKey("Login_msg_code4", PowerCreator.LMS.Language.LanagetItemType.Page));
            return false;
        }
        return true;
    }
    public string GetUserLocation()
    {
        try
        {
            WebClient webGetting = new WebClient();
            string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
            string startString = @"来自：";
            int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
            int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
            return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
        }
        catch
        {
            return string.Empty;
        }
    }
}
